import Vue from "vue";
import store from "@/store/index";
const v = new Vue();
export default {
  install(Vue) {
    Vue.prototype.$common = this;
  },

  /**
   *  树形表格分页
   * page.size条数 page.num 页数
   * @param {Object} page 分页参数值
   */
  treePaging(page) {
    let start = (Number(page.num) - 1) * Number(page.size);
    let end = Number(page.size) * Number(page.num);
    let trList = document.querySelectorAll(".tree-table .el-table__body tr");
    let trTree = document.querySelectorAll(
      ".tree-table .el-table__body .tree-tr"
    );
    //清空上次添加的class
    trTree.forEach((value) => {
      value.className = value.className.replace("tree-tr", "");
    });
    //记录总共显示了多少行
    let num = 0;
    trList.forEach((value) => {
      if (value.style.display != "none") {
        ++num;
        if (num <= start || num > end) {
          value.className += " tree-tr";
        }
      }
    });
    return num;
  },

  //刷新tab页
  refreshTab(n) {
    let tab = store.state.tabList;
    let a = n || store.state.activeTab;
    for (const i of tab) {
      if (i.name == a) {
        v.$set(i, "refresh", false);
        v.$nextTick(() => {
          v.$set(i, "refresh", true);
          v.$message.success(`${i.label}，刷新成功`);
        });
        break;
      }
    }
  },
  //新增tab页
  addTab(n) {
    store.commit("addTab", n);
  },
  closeTab(n) {
    store.commit("closeTab", n);
  },
  judgeTab(arr, n) {
    let s = JSON.parse(sessionStorage.getItem(n));
    if (s) {
      let flag = true;
      for (const i of s) {
        if (i.name == arr.name) {
          flag = false;
          break;
        }
      }
      flag && s.push(arr);
    } else {
      s = [arr];
    }
    sessionStorage.setItem(n, JSON.stringify(s));
    store.commit("addTab", arr);
  },
  //获取当前tab id
  getTabNameId(_this) {
    let id = _this.$parent.name.split("-");
    return id[1];
  },
  //获取当前tab name
  getTabName() {
    return store.state.activeTab;
  },
  //获取当前tab参数
  getTabParams() {
    let tab = store.state.tabList;
    let n = store.state.activeTab;
    let a = "";
    for (const i of tab) {
      if (i.name == n) {
        a = i.params;
        break;
      }
    }
    return a;
  },
  //获取当前tab
  getTab() {
    let tab = store.state.tabList;
    let n = store.state.activeTab;
    let a = "";
    for (const i of tab) {
      if (i.name == n) {
        a = i;
        break;
      }
    }
    return a;
  },
};
